import * as types from '../mutations'

import http from '../../http'

const state = {
  vm: '',
  order_list: [],
  total: 0,
  order_detail:{
    user:{},
    data:[
    ]
  },
  goods_list:[],
  user_list:[]
}

const getters = {
  order_list: state => state.order_list,
  total: state => state.total,
  order_detail: state => state.order_detail,
  goods_list: state => state.goods_list,
  user_list: state => state.user_list,
}

const mutations = {
  [types.SET_VM]: (state, payload) => {
    state.vm = payload.vm
  },
  [types.SET_ORDER_LIST]: (state, payload) => {
    state.order_list = payload.data
  },
  [types.SET_GOODS_LIST]: (state, payload) => {
    state.goods_list = payload.data
  },
  [types.SET_TOTAL]: (state, payload) => {
    console.log(payload.object)
    state.total = payload.object.total
  },
  [types.SET_ORDER_DETAIL]: (state, payload) => {
    console.log(payload)
    state.order_detail = payload
  },
  [types.SET_USER_LIST]: (state, payload) => {
    state.user_list = payload.data
  }
}

const actions = {
  get_order_list({
    commit,
    dispatch,
    state
  }, payload) {
    http('/api/order/query_order', payload, function (res) {
      commit('SET_ORDER_LIST', res)
      commit('SET_TOTAL', res)
    })
  },
  create_order({
    commit,
    dispatch,
    state
  }, payload) {
    http('/api/order/create_order', payload, function (res) {
      if(res.code === 0){
        state.vm.$message.success(res.message)
      }else{
        state.vm.$message.success('添加失败')
      }
      dispatch('get_order_list', state.vm.order_search)
      state.vm.new_order_visible = false
    })
  },
  modify_order({
    commit,
    dispatch,
    state
  }, payload) {
    http('/api/order/add_product', payload.modify, function (res) {
      state.vm.$message.success(res.message)
      dispatch('get_order_list', payload.order_search)
      dispatch('order_detail', payload.order_detail_search)
    })
  },
  order_detail({commit, dispatch, state}, payload) {
    http('/api/order/order_detail', payload, function (res) {
      commit('SET_ORDER_DETAIL', res.object )
    })
  },
  goods_search({commit, dispatch, state}, payload) {
    http('/api/product/query_product', payload, function (res) {
      commit('SET_GOODS_LIST', res)
    })
  },
  user_search({commit, dispatch, state}, payload) {
    http('/api/user/query_user', payload, res => {
      commit('SET_USER_LIST', res)
    })
  },
  edit_order({dispatch, commit, state}, payload) {
    http('/api/order/modify_order', payload.modify, res => {
      dispatch('order_search', payload.search)
    })
  },
  product_detail({dispatch, commit, state}, payload) {
    http('/api/product/product_detail', payload, res => {
      if(res.code === 0){
        state.vm.new_order_p.product.push({
          id: res.object.product.id,
          num: state.vm.new_o_add.num,
          name: res.object.product.name,
          price: res.object.product.price
        })
      }
    })
  },
  del_product(context, payload) {
    var vm = context.state.vm
    http('/api/order/del_product', payload, res => {
      vm.$message.success(res.message)
      context.dispatch('order_detail', {orderid: vm.order_detail.id})
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions

}
